﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.IO;
using WebVideo.Areas.Admin.Models;
using System.Collections;

namespace WebVideo.Areas.Admin.Models
{
    public class Questions_MultipleChoiceModel
    {
        public int Id { get; set; }

        //[Required(ErrorMessage = "Tên câu hỏi không được rỗng")]
        //[StringLength(50, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 5)]
        //public string Name { get; set; }

        [Required(ErrorMessage = "Nội dung không được để trống")]
        [StringLength(300, ErrorMessage = "Tên phải ít hơn {1} kí tự và lớn hơn {2} kí tự", MinimumLength = 10)]
        public string Des { get; set; }


        public int IdVideo { get; set; }

        public DateTime Date { get; set; }

        [Required(ErrorMessage = "Bạn chưa check câu trả lời")]
        public string Result { get; set; }

       [Required(ErrorMessage = "Không được rỗng")]
        public string QuesA { get; set; }
        [Required(ErrorMessage = "Không được rỗng")]
        public string QuesB { get; set; }
        [Required(ErrorMessage = "Không được rỗng")]
        public string QuesC { get; set; }
        [Required(ErrorMessage = "Không được rỗng")]
        public string QuesD { get; set; }

        public int Type { get; set; }

        public string Image { get; set; }
        // load
        public static IEnumerable<EntityQuestions_MultipleChoice> LayDuLieu(int IdEdit)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            var query = from p in db.EntityQuestions_MultipleChoices
                        where p.IdVideo == IdEdit
                        select p;

            return query.ToList();
        }

        public static void Insert(Questions_MultipleChoiceModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityQuestions_MultipleChoice tnew = new EntityQuestions_MultipleChoice();
            //tnew.Name = Mtnew.Name;
            tnew.Des = Mtnew.Des;
            if (Mtnew.IdVideo == 0)
            {
                tnew.IdVideo = null;
            }
            else
            {
                tnew.IdVideo = Mtnew.IdVideo;
            }
            tnew.Date = DateTime.Now;
            if (Mtnew.Image != null)
                tnew.Type = 1;
            else
                tnew.Type = 2;

            tnew.Image = Mtnew.Image;
            tnew.Result = Mtnew.Result;

            tnew.QuesA = Mtnew.QuesA ;
            tnew.QuesB = Mtnew.QuesB;
            tnew.QuesC = Mtnew.QuesC;
            tnew.QuesD = Mtnew.QuesD;
            db.EntityQuestions_MultipleChoices.InsertOnSubmit(tnew);
            
            db.SubmitChanges();

        }

        public static void InsertEmpty(int IdVideo)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityQuestions_MultipleChoice tnew = new EntityQuestions_MultipleChoice();

            tnew.Date = DateTime.Now;
            tnew.IdVideo = IdVideo;

            db.EntityQuestions_MultipleChoices.InsertOnSubmit(tnew);
            db.SubmitChanges();

        }

        public static Questions_MultipleChoiceModel LayModel(int IdQVideo)
        {
            Questions_MultipleChoiceModel Mtnew = new Questions_MultipleChoiceModel();


            dbVideoDataContext db = new dbVideoDataContext();

            EntityQuestions_MultipleChoice tnew = db.EntityQuestions_MultipleChoices.Where(p => p.Id == IdQVideo).Single();

            //Mtnew.Name = tnew.Name;
            Mtnew.Id = tnew.Id;
            Mtnew.Des = tnew.Des;
            Mtnew.IdVideo = (int)tnew.IdVideo;
            Mtnew.Image = tnew.Image;
            Mtnew.Result = tnew.Result;

            Mtnew.QuesA = tnew.QuesA;
            Mtnew.QuesB = tnew.QuesB;
            Mtnew.QuesC = tnew.QuesC;
            Mtnew.QuesD = tnew.QuesD;
            Mtnew.Type = (int)tnew.Type;
            //Mtnew.Point = (int)tnew.Point;

            return Mtnew;

        }
        public static string Edit(Questions_MultipleChoiceModel Mtnew)
        {
            dbVideoDataContext db = new dbVideoDataContext();

            EntityQuestions_MultipleChoice tnew = db.EntityQuestions_MultipleChoices.Where(p => p.Id == Mtnew.Id).Single();
            //tnew.Name = Mtnew.Name;
            tnew.Des = Mtnew.Des;
            tnew.IdVideo = Mtnew.IdVideo;
          
            string image = null;
            if (Mtnew.Image != null)
            {
                image = tnew.Image;
                tnew.Image = Mtnew.Image;
            }

            tnew.Result = Mtnew.Result;

            tnew.QuesA = Mtnew.QuesA;
            tnew.QuesB = Mtnew.QuesB;
            tnew.QuesC = Mtnew.QuesC;
            tnew.QuesD = Mtnew.QuesD;

            db.SubmitChanges();
            return image;

        }

        public static string Delete(int Id)
        {
            dbVideoDataContext db = new dbVideoDataContext();
            EntityQuestions_MultipleChoice tnew = db.EntityQuestions_MultipleChoices.Where(p => p.Id == Id).First();

            var query = db.EntityQuestions_MultipleChoices.Where(g => g.Type == 1 && g.IdVideo == tnew.IdVideo);// câu hỏi trắc ngiệm dành cho hình ảnh
            var query2 = db.EntityQuestions_MultipleChoices.Where(g => g.Type == 2 && g.IdVideo == tnew.IdVideo);// câu hỏi trắc ngiệm bình thường

           var query3 = db.EntityQuestions_Essays.Where(g => g.IdVideo == tnew.IdVideo);// câu hỏi tự luận
            
            if (query.ToList().Count > 0 && (query2.ToList().Count > 0 || query3.ToList().Count > 0))
            {
                db.EntityQuestions_MultipleChoices.DeleteOnSubmit(tnew);
                db.SubmitChanges();
            }
            return tnew.Image;

        }
    }
}